import {Component, ViewChild} from '@angular/core';

import { HomePage } from '../home/home';
import {SettingPage} from "../setting/setting";
import {Events, NavController, Platform, Tabs} from "ionic-angular";
import {BackButtonService} from "../../services/backButton.service";
import {LoginPage} from "../login/login";
import {AppInitService} from "../../services/appInit.service";

@Component({
  templateUrl: 'tabs.html'
})
export class TabsPage {

  tabRoots: Object[];
  @ViewChild('myTabs') tabRef: Tabs;
  // tab1Root = HomePage;
  // tab2Root = AboutPage;
  // tab3Root = ContactPage;

  constructor(public backButtonService: BackButtonService,
              private platform: Platform,
              private appInitService:AppInitService,
              private nav: NavController,
              private events: Events) {
    this.tabRoots = [
      {
        root: HomePage,
        tabTitle: '首页',
        tabIcon: 'home'
      },
      {
        root: SettingPage,
        tabTitle: '设置',
        tabIcon: 'person'
      }
      // {
      //   root: AboutPage,
      //   tabTitle: 'About',
      //   tabIcon: 'document'
      // }
    ];

    this.platform.ready().then(() => {
        //注册返回按钮监听
        this.backButtonService.registerBackButtonAction(this.tabRef);
        //登录后的初始化加载
        this.appInitService.initAfterLogin();
    });

  }

  listenEvents() {
    this.events.subscribe('toLogin',() => {
      console.log('返回登录');
      //this.viewCtrl.dismiss();
      this.nav.setRoot(LoginPage);
      // this.nav.pop(); 使用这种方式也可以，但是会在登录框中默认填上值
    })
  }

  ionViewDidLoad() {
    this.listenEvents();
    // console.log('界面创建');
  }

  ionViewWillUnload() {
    this.events.unsubscribe('toLogin');
    // console.log('界面销毁');
  }

}
